import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import '../resources.dart';

/// created by hcl
/// on 2024/02/23
/// page LoginTextField
/// desc 登录输入框
class LoginTextFieldColor extends StatefulWidget {
  TextEditingController textController;
  FocusNode focusNode;
  String hintText;
  Widget? leftImage;
  Widget? rightImage;
  double? rightWidth;
  int maxLines;
  int maxLength;
  bool enable;
  bool showPassword;
  bool textCenter;
  TextInputType keyboardType;
  List<TextInputFormatter>? inputFormatters;
  TextInputAction textInputAction;

  LoginTextFieldColor(
    this.textController,
    this.focusNode,
    this.hintText, {
    super.key,
    this.leftImage,
    this.rightImage,
    this.rightWidth,
    this.maxLines = 1,
    this.maxLength = 40,
    this.enable = true,
    this.showPassword = false,
    this.textCenter = false,
    this.keyboardType = TextInputType.text,
    this.inputFormatters,
    this.textInputAction = TextInputAction.done,
  });

  @override
  State<StatefulWidget> createState() => LoginTextFieldColorState();
}

class LoginTextFieldColorState extends State<LoginTextFieldColor> {
  bool isShowPassword = false;

  @override
  initState() {
    super.initState();
    if (widget.showPassword) {
      isShowPassword = true;
      if (mounted) setState(() {});
    }
  }

  showPasswordChange() {
    isShowPassword = !isShowPassword;
    if (mounted) setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      maxLines: widget.maxLines,
      maxLength: widget.maxLength,
      controller: widget.textController,
      focusNode: widget.focusNode,
      enabled: widget.enable,
      keyboardType: widget.keyboardType,
      obscureText: isShowPassword,
      textAlign: widget.textCenter ? TextAlign.center : TextAlign.start,
      textInputAction: widget.textInputAction,
      // 数字、手机号限制格式为0到9(白名单)， 密码限制不包含汉字（黑名单）
      inputFormatters: getInputFormatter(),
      style: normal316,
      decoration: InputDecoration(
        constraints: BoxConstraints.tightFor(height: 38.ss),
        counterText: '',
        hintText: widget.hintText,
        hintStyle: normal31606,
        filled: true,
        fillColor: ColorUtils.color_FFFFFF,
        border: _getBorder(),
        enabledBorder: _getBorder(),
        disabledBorder: _getBorder(),
        focusedBorder: _getBorder(),
        prefixIcon: Padding(
          padding: const EdgeInsets.only(left: 8.0),
          child: widget.leftImage ?? const SizedBox(),
        ),
        prefixIconConstraints: BoxConstraints(
            maxWidth: widget.leftImage != null ? 34.ss : 8.ss,
            maxHeight: 38.ss),
        suffixIcon: Padding(
          padding: const EdgeInsets.only(right: 8.0),
          child: widget.rightImage ?? const SizedBox(),
        ),
        suffixIconConstraints: BoxConstraints(
            maxWidth: widget.rightImage != null
                ? widget.rightWidth != null
                    ? widget.rightWidth!
                    : 34.ss
                : 8.ss,
            maxHeight: 38.ss),
      ),
    );
  }

  List<TextInputFormatter>? getInputFormatter() {
    if (widget.inputFormatters != null) {
      return widget.inputFormatters!;
    } else {
      List<TextInputFormatter> tempList = [];
      if (widget.keyboardType == TextInputType.number ||
          widget.keyboardType == TextInputType.phone) {
        tempList.add(FilteringTextInputFormatter.digitsOnly);
      }
      return tempList;
    }
  }

  OutlineInputBorder _getBorder() {
    return const OutlineInputBorder(
      borderSide: BorderSide(color: ColorUtils.color_333333),
      borderRadius: BorderRadius.all(Radius.circular(10.0)),
    );
  }

  Widget _buildShowPassword() {
    return widget.showPassword
        ? InkWell(
            onTap: () {
              showPasswordChange();
            },
            child: Container(
              width: 40.ss,
              height: 40.ss,
              alignment: Alignment.center,
              child: assetImage(
                  isShowPassword
                      ? ImageUtils.pwd_not_show
                      : ImageUtils.pwd_show,
                  width: 18.ss,
                  height: 16.ss),
            ),
          )
        : const SizedBox();
  }
}
